package com.helloae.chap09;

import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;

import com.esri.arcgis.carto.FeatureLayer;
import com.esri.arcgis.carto.esriSelectionResultEnum;
import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;
import com.esri.arcgis.geodatabase.ICursor;
import com.esri.arcgis.geodatabase.IFeature;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureCursor;
import com.esri.arcgis.geodatabase.IFeatureCursorProxy;
import com.esri.arcgis.geodatabase.IFeatureWorkspace;
import com.esri.arcgis.geodatabase.ISelectionSet;
import com.esri.arcgis.geodatabase.QueryFilter;
import com.esri.arcgis.geodatabase.SpatialFilter;
import com.esri.arcgis.geodatabase.esriSpatialRelEnum;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;

/**
 * 先用属性查询获得若干多边形，再查询完全被这些多边形包围的点。功能同"Select By Location"<br>
 * 示例章节：9.4.1,9.4.2<br>
 * <I>注意：这个类和书上的略有不同，书上的多边形来自于ArcMap的点选，并且只有一个，这里是通过条件查询获得多个多边形</I>
 * @author zhushijie
 *
 */
public class SpatialQuery {

	public static void main(String[] args) throws UnknownHostException,
			IOException {
		EngineInitializer.initializeVisualBeans();
		AoInitialize ao = new AoInitialize();
		ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);

		ShapefileWorkspaceFactory pWF = new ShapefileWorkspaceFactory();
		String ecliseWorkspace = System.getenv("Eclipse3.6Workspace");
		IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace) pWF
				.openFromFile(ecliseWorkspace + "HelloAE" + File.separator
						+ "DataSet" + File.separator + "chap09", 0);
		// 加载两个图层
		IFeatureClass pFeatureClass = pFeatureWorkspace
				.openFeatureClass("idcounty");
		IFeatureClass pCityClass = pFeatureWorkspace
				.openFeatureClass("idcities.shp");
		FeatureLayer pCityLayer = new FeatureLayer();
		pCityLayer.setFeatureClassByRef(pCityClass);

		FeatureLayer pFeatureLayer = new FeatureLayer();
		pFeatureLayer.setFeatureClassByRef(pFeatureClass);
		QueryFilter pQueryFilter = new QueryFilter();
		// 设置查询条件
		pQueryFilter.setWhereClause("Change > 70");
		// 查询并选中要素
		pFeatureLayer.selectFeatures(pQueryFilter,
				esriSelectionResultEnum.esriSelectionResultNew, false);

		ISelectionSet pCountySelection = pFeatureLayer.getSelectionSet();
		ICursor[] cursor = new ICursor[1];
		pCountySelection.search(null, true, cursor);
		IFeature pCounty = new IFeatureCursorProxy(cursor[0]).nextFeature();

		int cityNum = 0;
		//过滤条件会得到多个要素，这里while依次循环
		while (pCounty != null) {
			if (pCounty == null) {
				System.out.println("failed!");
			}

			SpatialFilter pSpatialFilter = new SpatialFilter();
			pSpatialFilter.setGeometryByRef(pCounty.getShape());
			//空间关系为完全包含
			pSpatialFilter.setSpatialRel(esriSpatialRelEnum.esriSpatialRelContains);

			IFeatureCursor pCityCursor = pCityClass.search(pSpatialFilter,
					false);
			IFeature pCity = pCityCursor.nextFeature();

			//每个要素可以包含多个点，循环处理
			while (pCity != null) {
				cityNum++;
				pCity = pCityCursor.nextFeature();
			}

			pCounty = new IFeatureCursorProxy(cursor[0]).nextFeature();
		}
		System.out.println(cityNum);
		ao.shutdown();
	}

}
